#############################################################################
# project: Prioritizing Exceptional Social Needs 
# file: 02_plot_function.R 
# author: Michael Jankowski, Brian Dietrich
# task: create plot functions for conjoint analysis
# input: /
# last revision: 2023/07/3
# output: plot_sw, plot_group_colour, plot_group_sw
############################################################################


### Plot function for simple illustration without comparison (black and white)

plot_sw <-function(data)              
{  
  ggplot(data,                              
         aes(y = estimate,            
             x = level)) +            
    geom_point(size=1.5, position=position_dodge(width=0.8)) +                                 
    geom_errorbar(aes(ymax = lower, ymin = upper, width = 0), position = position_dodge(0.8)) +  
    coord_flip() +                     
    theme_bw() +                      
    geom_hline(yintercept = 0.5,       
               colour = "black", 
               linetype = 2, 
               size = .7) +
    facet_grid(feature ~ .,                 
               scales  ="free_y", 
               space = "free_y", 
               switch = "both")+                                    
    scale_x_discrete(position="top") +                                                
    theme(plot.background = element_rect(fill = 'white'),           
          axis.text.x = element_text(size=11, color = "black"),     
          axis.text.y = element_text(size=8.5, color = "black"),    
          strip.text.y.left = element_text(angle = 0, color = "black", size = 8.5),  
          strip.background = element_rect(fill = 'white')) +        
    theme(axis.title.y = element_blank(),
          plot.margin=unit(c(1,1,1,3),"line")) +
    scale_y_continuous(breaks = seq(0.2,0.8, by =0.1)) +
    ylab("Marginal Means") +                                 
    theme(legend.position = "right") +                       
    scale_shape_manual(values = c(6, 1, 4)) 
}


### Plot function for group comparisons in color 

plot_group_colour <- function(data, Sample, vline = 0.5)
{
  ggplot(data, 
         aes(y = estimate, 
             x = level,
             group = Sample,      # Differenzierung nach Sample
             colour=Sample)) +
    geom_point(size=1.5, position=position_dodge(width=0.8)) +
    geom_errorbar(aes(ymax = lower, ymin = upper, width = 0), position = position_dodge(0.8)) +
    coord_flip() +
    theme_bw() +
    geom_hline(yintercept = vline, 
               colour = "black", 
               linetype = 2, 
               size = .7) +
    facet_grid(feature ~ ., 
               scales  ="free_y", 
               space = "free_y", 
               switch = "both")+
    scale_x_discrete(position="top") +
    theme(plot.background = element_rect(fill = 'white'),
          axis.text.x = element_text(size=11, color = "black"),
          axis.text.y = element_text(size=8.5, color = "black"),
          strip.text.y.left = element_text(angle = 0, color = "black", size = 8.5),
          strip.background = element_rect(fill = 'white')) +
    theme(axis.title.y = element_blank(),
          plot.margin=unit(c(1,1,1,3),"line")) +
    scale_y_continuous(breaks = seq(0.2,0.8, by =0.1)) +
    ylab("Marginal Means") +
    theme(legend.position = "bottom")
}


### Plot function for group comparison in black and white

plot_group_sw <- function(data, Sample, vline = 0.5)
{
  ggplot(data, 
         aes(y = estimate, 
             x = level,
             group = Sample,
             shape=Sample,
             linetype=Sample)) +
    geom_point(size=1.5, position=position_dodge(width=0.8)) +
    geom_errorbar(aes(ymax = lower, ymin = upper, width = 0), position = position_dodge(0.8)) +
    coord_flip() +
    theme_bw() +
    geom_hline(yintercept = vline, 
               colour = "black", 
               linetype = 2, 
               size = .7) +
    facet_grid(feature ~ ., 
               scales  ="free_y", 
               space = "free_y", 
               switch = "both")+
    scale_x_discrete(position="top") +
    theme(plot.background = element_rect(fill = 'white'),
          axis.text.x = element_text(size=11, color = "black"),
          axis.text.y = element_text(size=8.5, color = "black"),
          strip.text.y.left = element_text(angle = 0, color = "black", size = 8.5),
          strip.background = element_rect(fill = 'white')) +
    theme(axis.title.y = element_blank(),
          plot.margin=unit(c(1,1,1,3),"line")) +
    scale_y_continuous(breaks = seq(0.2,0.8, by =0.1)) +
    scale_linetype_manual(values = c("solid", "solid", "solid", "solid")) +
    ylab("Marginal Means") +
    theme(legend.position = "bottom") +
    scale_shape_manual(values = c(1, 6, 0, 8)) 
}

################################ End ##############################